import os

import pygame

from common import (add_score, black, blue, font_path, get_score,
                    go_next_level, red, white, gray)

font = pygame.font.Font(font_path, 30)

# 设置窗口
screen_width, screen_height = 1200, 500

# 加载图片
image_directory = "./assets/game_6/"  # 图片文件夹路径
image_path = os.path.join(image_directory, f"rate_limit.png")
image = pygame.image.load(image_path)
image = pygame.transform.scale(image, (1200, 350))

points = [
    (108, 150),
    (320, 150),
    (362, 182),
    (462, 182),
    (525, 232),
    (632, 232),
    (692, 280),
    (790, 280),
    (850, 350),
    (930, 350),
    (968, 380),
]
patch_point = (290, 150)

points_rect = [pygame.Rect(x - 8, y - 8, 16, 16) for x, y in points]

prompts = ["列车 2 入口速度控制曲线", "列车 2 出口速度控制曲线"]
questions = [
    "请依次选择图中黑色点按钮进行连线，并选择正确的按钮，绘制正确的列车 2 入口速度控制曲线",
    "请依次选择图中黑色点按钮进行连线，并选择正确的按钮，绘制正确的列车 2 出口速度控制曲线",
]
points_order = [
    [0, 1, 2, 4, 6, 8, 10],
    [0, 1, 3, 5, 7, 9, 10],
]
line_colors = [red, blue]

cur_level = 0
cur_point = 0
wrong_point = -1


# 游戏主循环
def draw(screen):
    global running, cur_level, cur_point, wrong_point

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == pygame.MOUSEBUTTONUP:
            if cur_point == len(points_order[cur_level]):
                cur_level += 1
                cur_point = 0

            for i, rect in enumerate(points_rect):
                if rect.collidepoint(event.pos):
                    if i == points_order[cur_level][cur_point]:
                        cur_point += 1
                        wrong_point = -1

                    elif cur_point > 0 and i > points_order[cur_level][cur_point - 1]:
                        add_score(-10)
                        wrong_point = i

    if cur_level >= len(prompts):
        go_next_level()
        return

    screen.fill(white)

    # draw prompt
    prompt = font.render(prompts[cur_level], True, black)
    screen.blit(prompt, (10, 10))

    # draw score at top right
    cur_score = get_score()
    score_text = font.render(f"当前得分：{cur_score}", True, red)
    screen.blit(score_text, (1000, 10))

    # draw image
    screen.blit(image, (0, 80))

    # draw patch_point with white color
    pygame.draw.circle(screen, white, patch_point, 10)

    # draw question
    question = font.render(questions[cur_level], True, black)
    screen.blit(question, (10, 450))

    # draw line between points
    for i in range(len(points_order[cur_level]) - 1):
        if i < cur_point - 1:
            pygame.draw.line(
                screen,
                line_colors[cur_level],
                points[points_order[cur_level][i]],
                points[points_order[cur_level][i + 1]],
                3,
            )

    if wrong_point != -1:
        pygame.draw.line(
            screen,
            gray,
            points[points_order[cur_level][cur_point - 1]],
            points[wrong_point],
            3,
        )
        # draw a x on the wrong point
        pygame.draw.line(
            screen,
            red,
            (points[wrong_point][0] - 5, points[wrong_point][1] - 5),
            (points[wrong_point][0] + 5, points[wrong_point][1] + 5),
            3,
        )
        pygame.draw.line(
            screen,
            red,
            (points[wrong_point][0] - 5, points[wrong_point][1] + 5),
            (points[wrong_point][0] + 5, points[wrong_point][1] - 5),
            3,
        )

    pygame.draw.circle(screen, black, points[1], 4)

    pygame.display.flip()
